package com.hnzq.cloud.sendassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.IO.AddDriverworkIO;
import com.hnzq.cloud.IO.DriverToAreaParam;
import com.hnzq.cloud.IO.entity.FreCar;
import com.hnzq.cloud.IO.entity.FreDriverwork;
import com.hnzq.cloud.sendassembly.mapper.FreDriverworkMapper;
import com.hnzq.cloud.sendassembly.service.FreCarService;
import com.hnzq.cloud.sendassembly.service.FreDriverworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnzq.cloud.util.ComUtil;
import com.hnzq.cloud.util.FreException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2022-02-07
 */
@Service
public class FreDriverworkServiceImpl extends ServiceImpl<FreDriverworkMapper, FreDriverwork> implements FreDriverworkService {
    @Autowired
    private FreCarService freCarService;

    @Override
    public FreDriverwork getById(Integer id) {
        QueryWrapper<FreDriverwork> freDriverworkQueryWrapper = new QueryWrapper<>();
        freDriverworkQueryWrapper.eq("driver_id",id);
        return getOne(freDriverworkQueryWrapper);
    }

    @Override
    public FreDriverwork getByUserId(Integer id) throws FreException {
        QueryWrapper<FreDriverwork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        FreDriverwork one = getOne(queryWrapper);
        if (one==null)
            throw new FreException("没有找到该司机工作信息",500);
        return one;
    }

    @Override
    public FreDriverwork getByArea(Integer id) throws FreException {
        QueryWrapper<FreDriverwork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_id",id);
        FreDriverwork one = getOne(queryWrapper);
        System.out.println(one);
        if (one==null)
            throw new FreException("没有找到该司机工作信息",500);
        return one;
    }

    @Override
    public boolean addFreDriverwork(AddDriverworkIO addDriverworkIO) {
        FreDriverwork freDriverwork = ComUtil.copyProperties(addDriverworkIO, FreDriverwork.class);
        try {
            FreCar byUserId = freCarService.getByUserId(freDriverwork.getUserId());
        } catch (FreException e) {
            return false;
        }
        return save(freDriverwork);
    }

    @Override
    public boolean editFreDriverwork(AddDriverworkIO addDriverworkIO) {

        FreDriverwork freDriverwork = ComUtil.copyProperties(addDriverworkIO, FreDriverwork.class);
        QueryWrapper<FreDriverwork> freDriverworkQueryWrapper = new QueryWrapper<>();
        freDriverworkQueryWrapper.eq("driver_id",freDriverwork.getDriverId());
        return update(freDriverwork,freDriverworkQueryWrapper);
    }

    @Override
    public boolean addArea(DriverToAreaParam driverToAreaParam) throws FreException {
        if (isDriverArea(driverToAreaParam.getAreaId())){
            throw new FreException("该区域已安排好司机",500);
        }
        FreDriverwork freDriverwork = ComUtil.copyProperties(driverToAreaParam, FreDriverwork.class);

        QueryWrapper<FreDriverwork> freDriverworkQueryWrapper = new QueryWrapper<>();
        freDriverworkQueryWrapper.eq("driver_id",freDriverwork.getDriverId());
        return update(freDriverwork,freDriverworkQueryWrapper);
    }
    public boolean isDriverArea(Integer areaId){
        QueryWrapper<FreDriverwork> freDriverworkQueryWrapper = new QueryWrapper<>();
        freDriverworkQueryWrapper.eq("area_id",areaId);
        List<FreDriverwork> list = list(freDriverworkQueryWrapper);
        if (list==null||list.size()<=0){
            return true;
        }
        return false;
    }
    @Override
    public boolean deleteDriverwork(Integer id) {
        FreDriverwork byId = getById(id);
        if (byId==null)
            return false;
        QueryWrapper<FreDriverwork> freDriverworkQueryWrapper = new QueryWrapper<>();
        freDriverworkQueryWrapper.eq("area_id",id);
        return remove(freDriverworkQueryWrapper);
    }
}
